/* Filename: client.c
 * Description: tcp basic版
 * Last modified: humble 2020-06-20 18:53
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "proto.h"

static void usage(void);

int main(int argc, char **argv)
{
    int sd;
    long long stamp;
    struct sockaddr_in raddr;
    FILE *fp;

    if(argc != 2){
        fprintf(stderr, "argc !=2");
        usage();
        exit(1);
    }

    sd = socket(AF_INET, SOCK_STREAM, 0/* IPPROTO_TCP,IPPROTO_SCTP */);
    if(sd < 0){
        perror("socket()");
        exit(1);
    }

    //bind(); //可省

    raddr.sin_family = AF_INET;
    raddr.sin_port = htons(atoi(SERVERPORT));
    inet_pton(AF_INET, argv[1], &raddr.sin_addr);

    if(connect(sd, (void *)&raddr, sizeof(raddr)) < 0){
        perror("socket()");
        exit(1);
    }

    fp = fdopen(sd, "r+");
    if(fp == NULL){
        //挂钩子函数
        perror("fdopen()");
        exit(1);
    }

    if(fscanf(fp, FMT_STAMP, &stamp) < 1){
        fprintf(stderr, "FMT_STAMP invalided\n");
    }
    else{
        fprintf(stdout, "stamp = %lld\n", stamp);
    }

    fclose(fp);

    //recv();
    //close();

    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  ./client <IP>\n"
    printf(USAGE);
}
